[ty] Use first matching constructor overload when inferring specializations#18204
[ty] Use first matching constructor overload when inferring specializations#18204
Conversation
| # TODO: revealed: C[bytes] | ||
| reveal_type(C(b"bytes")) # revealed: C[bytes | int] |
There was a problem hiding this comment.
Without the code changes, this test mimics the TemporaryDirectory behavior, returning a union specialization
|
| .and_then(|binding| combine_binding_specialization(db, binding)) | ||
| .into_iter() | ||
| .flat_map(CallableBinding::matching_overloads) | ||
| .next() | ||
| .and_then(|(_, binding)| binding.inherited_specialization()) |
There was a problem hiding this comment.
I think this is my mistake as I didn't revert this part from #17618 and only reverted the intersection of return type, sorry! 🙈
* main: [ty] Use first matching constructor overload when inferring specializations (#18204) [ty] Add hint that PEP 604 union syntax is only available in 3.10+ (#18192) Unify `Message` variants (#18051) [`airflow`] Update `AIR301` and `AIR311` with the latest Airflow implementations (#17985) [`airflow`] Move rules from `AIR312` to `AIR302` (#17940) [ty] Small LSP cleanups (#18201) [ty] Show related information in diagnostic (#17359) Default `src.root` to `['.', '<project_name>']` if the directory exists (#18141)
| @overload | ||
| def __init__(self: "C[bytes]", x: bytes) -> None: ... | ||
| @overload | ||
| def __init__(self: "C[int]", x: bytes) -> None: ... |
There was a problem hiding this comment.
Maybe not worth a separate follow-up PR, but wouldn't we ideally have a case where the second argument is optional and so we can have multiple matching overloads, and show that we pick the first?
There was a problem hiding this comment.
This gives us two matching overloads if you provide a bytes parameter, and we show that we pick the first (giving C[bytes] instead of C[int] or C[bytes | int]). Maybe deserves a comment, since the argument / specialization pattern across the overloads is subtle?
This is a follow-on to #18155. For the example raised in astral-sh/ty#370:
the new logic would notice that both overloads of
TemporaryDirectorymatch, and combine their specializations, resulting in an inferred type ofstr | bytes.This PR updates the logic to match our other handling of other calls, where we only keep the first matching overload. The result for this example then becomes
str, matching the runtime behavior. (We still do not implement the full overload resolution algorithm from the spec.)